package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.admin.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Api(tags = "通用功能")
@Slf4j
public class CommonController {

    @Autowired
    private CommonService commonService;

    @PostMapping("/admin/common/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) throws IOException {
        return commonService.upload(file);
    }

    @GetMapping("/courier/validateCodeToPhone")
    @ApiOperation("发送短信")
    public Result<String> sendSms(String telephone) throws Exception {
        log.info("发送短信的手机号为: {}", telephone);

        return commonService.sendSms(telephone);
    }

    @GetMapping("/dispatch/loadRouteInfo")
    public Result<String> loadRouteInfo(String tradeNo, Long courierId){
        log.info("用户查看商家 骑手 自己的位置信息: {}---{}", tradeNo, courierId);
        return commonService.loadRouteInfo(tradeNo, courierId);
    }

    @GetMapping("/r/{code}")
    public void shortUrl(@PathVariable("code") String code, HttpServletResponse response){
        log.info("用户请求短链接的编码: {}", code);

        //通过编码查询完整的链接地址
        String fullUri = commonService.queryFullUriByCode(code);

        //通过重定向的方式返回完整的地址
        try {
            response.sendRedirect(fullUri);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
